package state.component;

import particle.core.tools.PMath;
import state.GameConstants;
import state.GameImage;
import state.Graphics;
import state.Manager;
import state.Sprite;
import state.font.ImageText;
import stateframework_particle.view.StateFramework_ParticleResources;

public class Checkbox extends Component {
  private final int DX = 5;
  private final int DY = 3;

  private static GameImage checkedImage = null;
  private static GameImage uncheckedImage = null;

  protected EventListener listener = null;

  private boolean isChecked1 = false;
  private String label1 = null;
  private int textDistance = 0;
  private boolean drawLabelFirst = false;

  private int componentWidth = 0;

  public Checkbox(Manager manager1, EventListener listener1, Style style, int x1, int y1) {
    super(manager1, x1, y1, 100, 50);
    if (listener1 != null) {
      setListener(listener1);
    }

    if (style != null) {
      setStyle(style);
    }

    if (manager1 != null) {
      setManager(manager1);
    }
  }

  public void initComponent() {
    super.initComponent();
    label1 = "";
    textDistance = 5;

    if (checkedImage == null) {
      checkedImage = new GameImage(StateFramework_ParticleResources.image_checkbox_checked, null, null).setStatic(true);
      uncheckedImage = new GameImage(StateFramework_ParticleResources.image_checkbox_unchecked, null, null).setStatic(true);
    }
    _height = PMath.max(checkedImage.getHeight(), getFont().getHeight()) + DY * 2;
    calculateComponentWidth();
  }

  private void calculateComponentWidth() {
    componentWidth = checkedImage.getWidth() + textDistance + getFont().stringWidth(label1);
    setWidth(componentWidth + DX * 2 + 1);
  }

  public void draw(Graphics g) {
    if (isFillBackGround()) {
      if (isFocusable() && _focused) {
        g.setColor(getFocusBgColor());
      } else {
        g.setColor(getBackgroundColor());
      }
      g.fillRect(_realX - 1, _realY - 1, _width, _height);
    }

    int imageX = _realX + DX;
    int textX = _realX + checkedImage.getWidth() + textDistance + DX;

    if (drawLabelFirst) {
      imageX = _realX + getFont().stringWidth(label1) + textDistance + DX;
      textX = _realX + DX;
    }

    if (isChecked1) {
      g.drawImage(checkedImage, imageX, _realY + DY, GameConstants.TOP_LEFT_ANCHOR);
    } else {
      g.drawImage(uncheckedImage, imageX, _realY + DY, GameConstants.TOP_LEFT_ANCHOR);
    }

    int color = _forceGroundColor;
    if (isFocusable() && _focused) {
      color = _focusFgColor;
      if (_hasBorder) {
        g.setColor(borderColor);
        g.drawRect(_realX, _realY, _width - 1, _height - 1);
      }
    }
    getFont().drawString2(g, label1, color, textX, _realY, GameConstants.TOP_LEFT_ANCHOR);
  }

  public Checkbox setListener(EventListener listener1) {
    this.listener = listener1;
    return this;
  }

  public static void setCheckImage(GameImage checkedImage1) {
    Checkbox.checkedImage = checkedImage1;
  }

  public static void setUncheckImage(GameImage uncheckedImage1) {
    Checkbox.uncheckedImage = uncheckedImage1;
  }

  public boolean isChecked() {
    return isChecked1;
  }

  public Checkbox setChecked(boolean isChecked) {
    this.isChecked1 = isChecked;
    return this;
  }

  public String getLabel() {
    return label1;
  }

  public Checkbox setLabel(String label2) {
    this.label1 = label2;
    calculateComponentWidth();
    return this;
  }

  public int getTextDistance() {
    return textDistance;
  }

  public Checkbox setTextDistance(int textDistance1) {
    this.textDistance = textDistance1;
    calculateComponentWidth();
    return this;
  }

  public boolean isDrawLabelFirst() {
    return drawLabelFirst;
  }

  public Checkbox setDrawLabelFirst(boolean isDrawLabelFirst) {
    this.drawLabelFirst = isDrawLabelFirst;
    return this;
  }

  public Component setFont(ImageText font) {
    if (font == null) {
      return this;
    }
    _height = PMath.max(font.getHeight(), _height);
    return super.setFont(font);
  }

  public void pointerReleased(Sprite source, int x1, int y1) {
    setChecked(!isChecked());
    _manager.setFocus(this);
  }
}
